package com.test10_接口应用;

import org.junit.Test;

abstract class AbstractShape {
    public abstract double area();      // 计算面积
    public abstract double perimeter(); // 计算周长
}

class Circular3 extends AbstractShape {  // 定义圆形
    private double radius;

    public Circular3(double radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        return 3.14 * this.radius * this.radius;
    }

    @Override
    public double perimeter() {
        return 2 * 3.14 * this.radius;
    }
}

class Rectangle3 extends AbstractShape { // 定义矩形
    private double length;
    private double width;

    public Rectangle3(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double area() {
        return this.length * this.width;
    }

    @Override
    public double perimeter() {
        return 2 * (this.length + this.width);
    }
}

/**
 * 图形工厂
 */
class ShapeFactory {
    public static AbstractShape getInstance(String type, double... args) {
        if ("Circular".equalsIgnoreCase(type)) {
            return new Circular3(args[0]);
        } else if ("Rectangle".equalsIgnoreCase(type)) {
            return new Rectangle3(args[0], args[1]);
        }
        return null;
    }
}

public class YootkDemo_03 {

    @Test
    public void test1() {
        {
            AbstractShape shape = ShapeFactory.getInstance("Circular", 5.12);
            System.out.println("【圆形】面积：" + String.format("%.4f", shape.area()) + "、周长：" + String.format("%.4f", shape.perimeter()));
        }
        {
            AbstractShape shape = ShapeFactory.getInstance("Rectangle", 5.12, 78.92);
            System.out.println("【矩形】面积：" + shape.area() + "、周长：" + shape.perimeter());
        }
    }
}
